﻿using EPiAbstractions.Community.Blog;
using EPiServer.Common.Security;
using EPiServer.Community.Blog;
using JoelAbrahamsson.TestUtilities;
using Moq;
using StructureMap.AutoMocking;
using Xunit;

namespace JoelAbrahamsson.MetaWeblog.Tests
{
    public class DeletePostTests
    {
        [Fact]
        public void GivenPostIDForExistingEntry_DeletePost_RemovesEntry()
        {
            int postID = 1;
            Blog blog = new Mock<Blog>().Object;
            Mock<Entry> mockEntry = new Mock<Entry>();
            mockEntry.Setup(e => e.Blog).Returns(blog);
            Entry entry = mockEntry.Object;
            MoqAutoMocker<MetaWeblogHandler> metaWebLogHandlerAutoMocker = new MoqAutoMocker<MetaWeblogHandler>();
            metaWebLogHandlerAutoMocker.PartialMockTheClassUnderTest();
            Mock<IBlogHandlerFacade> fakeBlogHandlerFacade = Mock.Get(metaWebLogHandlerAutoMocker.Get<IBlogHandlerFacade>());
            fakeBlogHandlerFacade.Setup(facade => facade.RemoveEntry(entry));
            Mock<MetaWeblogHandler> partiallyFakedMetaWeblogHandler = Mock.Get(metaWebLogHandlerAutoMocker.ClassUnderTest);
            partiallyFakedMetaWeblogHandler.Setup(handler => handler.GetEntry(postID.ToString())).Returns(entry);
            partiallyFakedMetaWeblogHandler.Setup(
                handler => handler.GetValidatedUser(blog, It.IsAny<string>(), It.IsAny<string>())).Returns((IUser)null);
            
            bool result = metaWebLogHandlerAutoMocker.ClassUnderTest.DeletePost(null, postID.ToString(), null, null, true);

            fakeBlogHandlerFacade.Verify(facade => facade.RemoveEntry(entry));
            Assert.True(result);
        }

        [Fact]
        public void GivenEntryWithPostIDDoesNotExist_DeletePost_ReturnsFalse()
        {
            int postID = 1;
            Blog blog = new Mock<Blog>().Object;
            IUser user = new Mock<IUser>().Object;
            MoqAutoMocker<MetaWeblogHandler> metaWebLogHandlerAutoMocker = new MoqAutoMocker<MetaWeblogHandler>();
            metaWebLogHandlerAutoMocker.PartialMockTheClassUnderTest();
            Mock<MetaWeblogHandler> partiallyFakedMetaWeblogHandler = Mock.Get(metaWebLogHandlerAutoMocker.ClassUnderTest);
            partiallyFakedMetaWeblogHandler.Setup(handler => handler.GetEntry(postID.ToString())).Returns((Entry)null);
            partiallyFakedMetaWeblogHandler.Setup(
                handler => handler.GetValidatedUser(blog, It.IsAny<string>(), It.IsAny<string>())).Returns(user);

            bool result = partiallyFakedMetaWeblogHandler.Object.DeletePost(null, postID.ToString(), null, null, true);

            Assert.False(result);
        }

        [Fact]
        public void DeletePost_ValidatesUser()
        {
            string username = RandomValues.String;
            string password = RandomValues.String;
            int postID = 1;
            IUser user = new Mock<IUser>().Object;
            Entry entry = new Mock<Entry>().Object;
            MoqAutoMocker<MetaWeblogHandler> metaWebLogHandlerAutoMocker = new MoqAutoMocker<MetaWeblogHandler>();
            metaWebLogHandlerAutoMocker.PartialMockTheClassUnderTest();
            Mock<IBlogHandlerFacade> fakeBlogHandlerFacade = Mock.Get(metaWebLogHandlerAutoMocker.Get<IBlogHandlerFacade>());
            fakeBlogHandlerFacade.Setup(facade => facade.RemoveEntry(entry));
            Mock<MetaWeblogHandler> partiallyFakedMetaWeblogHandler = Mock.Get(metaWebLogHandlerAutoMocker.ClassUnderTest);
            partiallyFakedMetaWeblogHandler.Setup(handler => handler.GetEntry(postID.ToString())).Returns(entry);
            partiallyFakedMetaWeblogHandler.Setup(
                handler => handler.GetValidatedUser(entry.Blog, It.IsAny<string>(), It.IsAny<string>())).Returns(user);

            partiallyFakedMetaWeblogHandler.Object.DeletePost(null, postID.ToString(), username, password, true);

            partiallyFakedMetaWeblogHandler.Verify(handler => handler.GetValidatedUser(entry.Blog, username, password));
        }
    }
}
